package cruds;

import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.model.AbstractReadOnlyModel;

import home.Home;
import panels.BodyPanel;
import services.GenericService;
import services.StockableService;
import staticmenus.StaticMenu;

@SuppressWarnings("serial")
public abstract class ParentList<T> extends BodyPanel{

	private GenericService<T> service;
	private List<T> items;
	
	public ParentList(Home home, List<T> items) {
		super(home);
		this.items = items;
		Form form = new Form("form");
		this.add(form);
		form.setOutputMarkupId(true);
		this.initializePanelTH(form);
	    this.initialize(form);
	}
	
	public ParentList(Home home) {
		super(home);
		Form form = new Form("form");
		this.add(form);
		form.setOutputMarkupId(true);
		this.initializePanelTH(form);
	    this.initialize(form);
	}


	protected abstract void initializePanelTH(Form form);

	protected void initialize(Form form){
		this.initializeService();
		DataView<T> dataView = new DataView<T>("items", this.setDataProvider()){

			@Override
			protected void populateItem(final Item<T> item) {
				ParentList.this.createFormFields(item);
				ParentList.this.createDeleteButton(item);
				ParentList.this.createModifyButton(item);
				
				item.add(AttributeModifier.replace("class", new AbstractReadOnlyModel<String>() {
	                 private static final long serialVersionUID = 1L;

	                    @Override
	                    public String getObject() {
	                        return item.getIndex() % 2 == 1 ? "even" : "odd";
	                    }
	            }));
			}
			
		};
		  dataView.setItemsPerPage(8);
	      form.add(dataView);
	      form.add(new PagingNavigator("navigator", dataView));
	};

	protected void createModifyButton(final Item<T> item) {
		 item.add(new AjaxButton("modify") {
				@Override
				protected void onSubmit(AjaxRequestTarget target,Form<?> form) {
					ParentList.this.doUpdateAndRedirect(target,item);
					ParentList.this.updatePage(target);	
				}
				
				@Override
				protected void onError(AjaxRequestTarget target,Form<?> form) {
						
				}
		 });
	}


	protected void createDeleteButton(final Item<T> item) {
		item.add(new AjaxButton("delete") {
			@Override
			protected void onSubmit(AjaxRequestTarget target,Form<?> form) {
				ParentList.this.doDeleteAndRedirect(target, item);
				ParentList.this.updatePage(target);	
			}
			@Override
			protected void onError(AjaxRequestTarget target,Form<?> form) {
					
			}
		 });
	}

	protected void updatePage(AjaxRequestTarget target){
		this.updateDynamicMenu(target);
		StaticMenu menustatico = new StaticMenu(ParentList.this.getHome());
		menustatico.changePanel(target);
	}

	protected abstract void updateDynamicMenu(AjaxRequestTarget target);

	protected abstract void doUpdateAndRedirect(AjaxRequestTarget target, Item<T> item);
	
	protected abstract void doDeleteAndRedirect(AjaxRequestTarget target, Item<T> item);
	
	protected abstract void createFormFields(Item<T> item);

	protected abstract void initializeService();
	
	protected abstract IDataProvider setDataProvider();

	//--------Sett&Gett
	
	public GenericService<T> getService() {
		return service;
	}

	public void setService(GenericService<T> service) {
		this.service = service;
	}
	
	public List<T> getItems() {
		return items;
	}

	public void setItems(List<T> items) {
		this.items = items;
	}

}
